package net.wantongtech.xa.uploadbasicinfo.service;

import cn.com.taiji.sdk.tools.JsonTools;
import com.google.common.collect.Sets;
import com.google.common.io.Files;
import com.wtkj.zwxt.clearing.vo.dispute.DisputeTransactionModel;
import com.wtkj.zwxt.clearing.vo.dispute.EtcDisputeModel;
import com.wtkj.zwxt.xxjh.share.state.TradeStaticValue;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.dao.CheckMissingDisputedTransDao;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 检查缺失的争议交易服务实现
 *
 * @author MaShibo
 * 日期 :  2020/3/10
 */
@Service
@Slf4j
public class CheckMissingDisputedTransService {


    @Resource
    private CheckMissingDisputedTransDao checkMissingDisputedTransDao;


    @SuppressWarnings("UnstableApiUsage")
    public void check() {
        try {
            File file = new File("C:\\Users\\Administrator\\Documents\\Cj0eUV5ZraaANSK-AT7Zf-wcVtk88.json");
            String json = Files.toString(file, Charset.forName("UTF-8"));
            EtcDisputeModel etcDisputeModel = JsonTools.json2Object(json, EtcDisputeModel.class);
            List<String> transactionIdList = etcDisputeModel.getMessageList()
                    .stream()
                    .flatMap(v -> v.getTransaction().stream())
                    .map(DisputeTransactionModel::getTransactionId)
                    .collect(Collectors.toList());
            log.info("共{}条原始交易数据", transactionIdList.size());

            Set<String> errorTransIdSet = Sets.newHashSet();

            Set<String> outProvinceTransIdSet = Sets.newHashSet();

            transactionIdList.forEach(transactionId -> {
                Integer state = checkMissingDisputedTransDao.queryPortalTransOutState(transactionId);
                if (state == null || state != TradeStaticValue.TradeState.DSPT_ACCOUNT) {
                    errorTransIdSet.add(transactionId);

                    boolean outProvinceTrans = checkMissingDisputedTransDao.isOutProvinceTrans(transactionId);
                    if (!outProvinceTrans) {
                        outProvinceTransIdSet.add(transactionId);
                    }
                }
            });

            log.info("未查找到的交易编号：{}", errorTransIdSet);
            log.info("外省交易编号：{}", outProvinceTransIdSet);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }


    @SuppressWarnings("UnstableApiUsage")
    public void checkV2() {
        try {
            File file = new File("E:\\tools\\610103清分交易.txt");
            List<String> transactionIdList = Files.readLines(file, Charset.forName("UTF-8"));
            log.info("从文件中获取{}个交易ID", transactionIdList.size());
            List<String> transactionIdFromDBList = checkMissingDisputedTransDao.queryBOCDisputedTransId();
            log.info("从数据库获取{}个交易ID", transactionIdFromDBList.size());
            Collection<String> subtract = CollectionUtils.subtract(transactionIdList, transactionIdFromDBList);
            log.info("差集：{}", subtract);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }


    }
}
